package com.jdew.thkj.activity;

import android.text.TextUtils;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;

import com.alibaba.fastjson.JSONObject;
import com.jdew.thkj.R;
import com.jdew.thkj.base.BaseActivity;
import com.jdew.thkj.manager.AppConfig;
import com.jdew.thkj.network.RetrofitManager;
import com.jdew.thkj.network.api.RequestApi;
import com.jdew.thkj.utils.MD5Utils;
import com.jdew.thkj.utils.RegexValidateUtils;
import com.jdew.thkj.utils.ToastUtils;

import butterknife.Bind;
import rx.Subscriber;
import rx.android.schedulers.AndroidSchedulers;
import rx.schedulers.Schedulers;

/**
 * Created by yangzai on 2018/3/2 0002.
 */

public class ChongZhiActivity extends BaseActivity {
    @Bind(R.id.back)
    ImageView back;
    @Bind(R.id.title)
    TextView title;
    @Bind(R.id.phone)
    EditText phone;
    @Bind(R.id.code)
    EditText code;
    @Bind(R.id.chongzhi)
    TextView chongzhi;

    @Override
    public int bindLayout() {
        return R.layout.activity_chongzhi;
    }

    @Override
    public void initView(View view) {
        title.setText("余额充值");
        back.setVisibility(View.VISIBLE);
        back.setOnClickListener(this);
        chongzhi.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.back: {
                finish();
            }
            break;
            case R.id.chongzhi: {
                SetUserMoney();
            }
            break;
            default:
                break;
        }
    }

    public void SetUserMoney() {
        if(TextUtils.isEmpty(phone.getText())){
            ToastUtils.getInstance(this).showMessage("请输入您要充值的手机号码");
            return;
        }
        else if(TextUtils.isEmpty(code.getText())){
            ToastUtils.getInstance(this).showMessage("请输入充值码");
            return;
        }
        String timesamp=RegexValidateUtils.getTime13();
        String numsamp=RegexValidateUtils.getRandom10();
        RetrofitManager.getInstance( AppConfig.BASE_URL).createReq(RequestApi.class).SetUserMoney(phone.getText().toString().trim(),code.getText().toString().replace(" ",""), RegexValidateUtils.getUniquePsuedoID(),timesamp,numsamp,MD5Utils.getMD5(RegexValidateUtils.ASCIISort(timesamp+numsamp+RegexValidateUtils.getUniquePsuedoID()+sp.getString("SignToken","")+"UserPhone"+phone.getText().toString().trim()+"Code"+code.getText().toString().replace(" ",""))).toUpperCase()).observeOn( AndroidSchedulers.mainThread()).subscribeOn( Schedulers.io()).subscribe(new Subscriber<JSONObject>() {
            @Override
            public void onCompleted() {

            }
            @Override
            public void onError(Throwable e) {
            }
            @Override
            public void onNext(JSONObject obj) {
                if (obj.getString("code").equals("0")) {
                    ToastUtils.getInstance(ChongZhiActivity.this).showMessage("充值成功");
                    finish();
                }else if(obj.getString("code").equals("-1")){
                    ToastUtils.getInstance(ChongZhiActivity.this).showMessage("该手机号无法充值");
                }else if(obj.getString("code").equals("-2")){
                    ToastUtils.getInstance(ChongZhiActivity.this).showMessage("无效的充值码");
                }else if(obj.getString("code").equals("-3")){
                    ToastUtils.getInstance(ChongZhiActivity.this).showMessage("充值失败");
                }
            }
        } );
    }
}
